/*
 * File: stack.c
 * Author:  Yin Lijun <yinlijun2004@gmail.com>
 * Brief:   stack implement from darray.
 *
 * Copyright (c) 2011 - 2012  Yin Lijun <yinlijun2004@gmail.com>
 *
 * Licensed under the Academic Free License version 2.1
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * History:
 * ================================================================
 * 2012-03-12 Yin Lijun <yinlijun2004@gmail.com> created
 *
 */
#include "darray.h"
#include "stack_from_darray.h"

struct _Stack
{
	DArray* darray;
};

Stack* stack_create(DestoryFunc dest)
{
	Stack* thiz = (Stack *)malloc(sizeof(Stack));
	return_if_fail_val(thiz != NULL, NULL);

	thiz->darray = darray_create(dest);
	if(thiz->darray == NULL)
	{
		free(thiz);
		thiz = NULL;
	}
	
	return thiz;
}

Ret stack_push_data(Stack* thiz, void* data)
{
	return_if_fail_val(thiz != NULL, RET_PARAM_ERROR);
	
	return darray_append(thiz->darray, data);
}

void* stack_pop_data(Stack* thiz)
{
	return_if_fail_val(thiz != NULL, NULL);
	size_t length = darray_get_length(thiz->darray);
	void *data = NULL;
   	darray_get_data_by_index(thiz->darray, length - 1, &data);
	darray_delete(thiz->darray, length - 1);

	return data;
}

Ret stack_get_top_data(Stack* thiz, void** data)
{
	return_if_fail_val(thiz != NULL
					&& data != NULL,
					RET_PARAM_ERROR);
	size_t length = darray_get_length(thiz->darray);
   	return darray_get_data_by_index(thiz->darray, length - 1, data);

}

size_t stack_get_length(Stack* thiz)
{
	return_if_fail_val(thiz != NULL, RET_PARAM_ERROR);
	
	return darray_get_length(thiz->darray);
}

Ret stack_foreach(Stack* thiz, VisitFunc visit, void* ctx)
{
	return_if_fail_val(thiz != NULL, RET_PARAM_ERROR);

	return darray_foreach(thiz->darray, visit, ctx);
}

Ret stack_destroy(Stack* thiz)
{
	return_if_fail_val(thiz != NULL, RET_PARAM_ERROR);
	darray_destroy(thiz->darray);

	free(thiz);
	
	return RET_OK;
}

#ifdef __STACK_TEST__
#include <stdio.h>
#include <assert.h>
#define TEST_SIZE 1000
void int_visit(void* ctx, void* data)
{
	printf("%4d", (int)data);
}

int main(int argc, char** argv)
{
	Stack* stack = stack_create(NULL);
	assert(stack != NULL);
	
	int i = 0;
	int length = 0;

	printf("\ntest stack_push_data\n");
	for(i = 0; i < TEST_SIZE; i++)
	{
		assert(stack_push_data(stack, (void *)i) == RET_OK);
		assert(stack_get_length(stack) == ++length);
	}	
	stack_foreach(stack, int_visit, NULL);
	
	printf("\ntest stack_pop_data\n");
	for(i = 0; i < TEST_SIZE; i++)
	{
		int j = (int)stack_pop_data(stack);
		assert(stack_get_length(stack) == --length);
		assert(j == TEST_SIZE - i - 1);
	}
	stack_foreach(stack, int_visit, NULL);
	
	printf("\ntest stack_push_data\n");
	for(i = 0; i < TEST_SIZE; i++)
	{
		assert(stack_push_data(stack, (void *)i) == RET_OK);
		assert(stack_get_length(stack) == ++length);
	}
	stack_foreach(stack, int_visit, NULL);

	printf("\ntest stack_destroy\n");
	assert(stack_destroy(stack) == RET_OK);
	printf("\n");

	return 0;
}
#endif
